/*************************************************************************
> File Name: one_way_linked_list.c
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-07-24 19:01:57
> Description: 单向链表
************************************************************************/
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "one_way_linked_list.h"

static Node *createNode()
{
	Node *node = malloc(sizeof(Node));
	if (node == NULL)
	{
		return NULL;
		perror("Create Node Error!");
	}
	// 对空间进行清空
	memset(node, 0, sizeof(Node));
	node->next = NULL;
	return node;
}
/// @brief 创建链表
/// @return 返回头结点
Node *createHead()
{
	/*
	 * 1.定义一个头结点的指针
	 * 2.为指针分配内存地址
	 * 3.将指针的指向内容的值清空
	 * 4.给头结点的下一个节点赋值为空
	 * */
	Node *head;
	head = createNode();
	return head;
}
/// @brief 获取节点数
/// @param head 头结点
/// @return 返回节点数
int getNodeCount(Node *head)
{
	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return 0;
	}
	int count = 0;
	while (head->next != NULL)
	{
		head = head->next;
		count++;
	}
	printf("linked list length = %d\n", count);
	return count;
}
/// @brief 尾插法
/// @param head 头节点
/// @param item 插入值
/// @return 失败返回NULL 成功返回头结点
Node *insertTailNode(Node *head, int item)
{
	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return NULL;
	}
	Node *node = createNode();
	node->data.value = item;
	// node->next = head;
	Node *p;
	p = head;
	while (p->next != NULL)
	{
		// 循环到最后一个节点
		p = p->next;
	}
	p->next = node;
	return head;
}
/// @brief 头插法
/// @param head 头结点
/// @param item 目标值
/// @return 失败返回NULL 成功返回头结点
Node *insertHeadNode(Node *head, int item)
{
	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return NULL;
	}
	Node *node = createNode();
	node->data.value = item;
	node->next = head->next;
	head->next = node;
	return head;
}
/// @brief 插入到第n个位置后
/// @param head 头节点
/// @param location 位置n
/// @param item 插入值
/// @return 失败返回NULL 成功返回头结点
Node *insertMidNode(Node *head, int location, int item)
{

	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return NULL;
	}
	Node *node = createNode();
	node->data.value = item;
	Node *p, *front_node;
	p = head;
	int count = getNodeCount(head);
	if (location + 1 <= count)
	{
		for (int i = 0; i < location; i++)
		{
			front_node = p;
			p = p->next;
		}
		front_node->next = node;
		node->next = p;
	}
	else
	{
		perror("Linked List Over Bound");
	}
	return head;
}
//

/// @brief 移除第n个位置
/// @param location 位置n
/// @param head 	头结点
/// @return 失败返回NULL,成功返回头节点
Node *removeNode(Node *head, int location)
{
	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return NULL;
	}
	Node *p, *front_node;
	p = head;
	int count = getNodeCount(head);
	if (location + 1 <= count)
	{
		for (int i = 0; i < location; i++)
		{
			front_node = p;
			p = p->next;
		}
		front_node->next = p->next;
		free(p);
	}
	else
	{
		perror("Linked List Over Bound");
	}
	return head;
}
/// @brief 释放链表
/// @param head 头结点
/// @return 释放失败返回-1，成功返回1;
Node *freeLinkedList(Node *head)
{
	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return NULL;
	}
	Node *current = head;
	Node *next;
	while (current != NULL)
	{
		next = current->next;
		free(current);
		current = next;
	}
	head = NULL;
	return head;
}
/// @brief 打印链表
/// @param head 头节点
void printList(Node *head)
{
	if (head == NULL)
	{
		perror("Linked List Is NULL!");
		return;
	}
	int i = 0;
	while (head->next != NULL)
	{
		i++;
		head = head->next;
		printf("node %d = {%d}\t", i, head->data.value);
	}
	printf("\n");
}